package com.zzptc.sky.bdsjws.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzptc.sky.bdsjws.MyApplication;
import com.zzptc.sky.bdsjws.R;
import com.zzptc.sky.bdsjws.activites.FamilyProtectActivity;
import com.zzptc.sky.bdsjws.activites.MainActivity;
import com.zzptc.sky.bdsjws.activites.OneKeySosActivity;
import com.zzptc.sky.bdsjws.vo.Bbx;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class Baoxiang_Adapter extends RecyclerView.Adapter<Baoxiang_Adapter.Mybaoxiang_VH> {


     List<Bbx> list;

    public Baoxiang_Adapter(List<Bbx> list) {
        this.list = list;
    }

    public class Mybaoxiang_VH extends RecyclerView.ViewHolder {
//        @BindView(R.id.bx_rv)
//        RecyclerView bx_rv;
        @BindView(R.id.bx_tv)
         TextView bx_tv;
        @BindView(R.id.bx_iv)
         ImageView bx_iv;

        public Mybaoxiang_VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
//            bx_tv = (TextView) itemView.findViewById(R.id.bx_tv);
//            bx_iv = (ImageView) itemView.findViewById(R.id.bx_iv);
        }
    }

    @Override
    public Mybaoxiang_VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bbx_item, parent, false);
        Mybaoxiang_VH mybaoxiang_vh = new Mybaoxiang_VH(view);
        return mybaoxiang_vh;
    }

    @Override
    public void onBindViewHolder(Mybaoxiang_VH holder, int position) {
        final Bbx bbx = list.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bbx.getName().equals("家人守护")) {
                    Intent intent = new Intent(MyApplication.getContext(), FamilyProtectActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                } else if (bbx.getName().equals("回首页")) {
                    Intent intent = new Intent(MyApplication.getContext(), MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                }
            }
        });
        //先解决：将数据库中取得的pic图片的String------>Drawable  准备：放图片到res
        Context context = MyApplication.getContext();
        int iv_int = context.getResources().getIdentifier(bbx.getIcon(), "mipmap", context.getPackageName());
        try {


            // Drawable drawable=context.getResources().getDrawable(iv_int);
            Drawable drawable = ContextCompat.getDrawable(context, iv_int);
            holder.bx_iv.setImageDrawable(drawable);

        }catch (Exception e){
            e.printStackTrace();
        }

        holder.bx_tv.setText(bbx.getName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

}
